document.addEventListener('DOMContentLoaded', function() {
    // 导航菜单切换
    const navLinks = document.querySelectorAll('.main-nav a');
    const sections = document.querySelectorAll('.section');
    
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            
            // 移除所有active类
            navLinks.forEach(l => l.classList.remove('active'));
            sections.forEach(s => s.classList.remove('active-section'));
            
            // 添加active类到当前链接
            this.classList.add('active');
            
            // 显示对应的部分
            const targetId = this.getAttribute('href');
            document.querySelector(targetId).classList.add('active-section');
            
            // 平滑滚动
            if(targetId !== '#about') {
                document.querySelector(targetId).scrollIntoView({
                    behavior: 'smooth'
                });
            } else {
                window.scrollTo({
                    top: 0,
                    behavior: 'smooth'
                });
            }
        });
    });
    
    // 返回顶部按钮
    const backToTopBtn = document.getElementById('backToTop');
    
    window.addEventListener('scroll', function() {
        if (window.pageYOffset > 300) {
            backToTopBtn.style.display = 'flex';
        } else {
            backToTopBtn.style.display = 'none';
        }
    });
    
    backToTopBtn.addEventListener('click', function() {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });
    
    // 表单提交
    const contactForm = document.getElementById('contactForm');
    
    if(contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单数据
            const formData = new FormData(this);
            const data = {};
            formData.forEach((value, key) => {
                data[key] = value;
            });
            
            // 这里可以添加AJAX请求来发送表单数据
            console.log('表单数据:', data);
            
            // 显示成功消息
            alert('感谢您的留言! 我会尽快回复您。');
            this.reset();
        });
    }
    
    // 动画效果 - 当元素进入视口时触发
    const animateOnScroll = function() {
        const elements = document.querySelectorAll('.timeline-item, .project-card, .cert-item, .interest-item');
        
        elements.forEach(element => {
            const elementPosition = element.getBoundingClientRect().top;
            const windowHeight = window.innerHeight;
            
            if (elementPosition < windowHeight - 100) {
                element.style.opacity = '1';
                element.style.transform = 'translateY(0)';
            }
        });
    };
    
    // 初始化元素样式
    const initAnimations = function() {
        const elements = document.querySelectorAll('.timeline-item, .project-card, .cert-item, .interest-item');
        
        elements.forEach((element, index) => {
            element.style.opacity = '0';
            element.style.transform = 'translateY(20px)';
            element.style.transition = `opacity 0.5s ease ${index * 0.1}s, transform 0.5s ease ${index * 0.1}s`;
        });
        
        // 触发一次检查
        animateOnScroll();
    };
    
    initAnimations();
    window.addEventListener('scroll', animateOnScroll);
    
    // 技能条动画
    const skillBars = document.querySelectorAll('.bar');
    
    const animateSkillBars = function() {
        skillBars.forEach(bar => {
            const level = bar.getAttribute('data-level');
            bar.style.width = level + '%';
        });
    };
    
    // 使用Intersection Observer来触发技能条动画
    const skillsSection = document.querySelector('.skills');
    
    if(skillsSection) {
        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    animateSkillBars();
                    observer.unobserve(entry.target);
                }
            });
        }, { threshold: 0.5 });
        
        observer.observe(skillsSection);
    }
});
// 响应式调整
function handleResponsive() {
    const backToTopBtn = document.getElementById('backToTop');
    const printBtn = document.getElementById('printResume');
    
    if (window.innerWidth <= 575) {
        // 移动端小屏幕优化
        if (backToTopBtn) {
            backToTopBtn.innerHTML = '<i class="fas fa-arrow-up"></i>';
        }
        if (printBtn) {
            printBtn.innerHTML = '<i class="fas fa-print"></i>';
        }
    } else {
        // PC端恢复
        if (backToTopBtn) {
            backToTopBtn.innerHTML = '<i class="fas fa-arrow-up"></i> 返回顶部';
        }
        if (printBtn) {
            printBtn.innerHTML = '<i class="fas fa-print"></i> 打印简历';
        }
    }
}

// 初始执行
handleResponsive();

// 窗口大小变化时执行
window.addEventListener('resize', handleResponsive);
// 移动端触摸反馈
document.querySelectorAll('a, button').forEach(element => {
    element.addEventListener('touchstart', function() {
        this.style.transform = 'scale(0.95)';
    });
    element.addEventListener('touchend', function() {
        this.style.transform = '';
    });
});

// 防止触摸时触发hover效果
document.addEventListener('touchstart', function() {}, true);